{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "11af867b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.model_selection import train_test_split\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "297d998a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>V1</th>\n",
       "      <th>V2</th>\n",
       "      <th>V3</th>\n",
       "      <th>V4</th>\n",
       "      <th>V5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2520.35</td>\n",
       "      <td>277.115</td>\n",
       "      <td>51.4808</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1702.40</td>\n",
       "      <td>282.350</td>\n",
       "      <td>62.1260</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1917.65</td>\n",
       "      <td>345.125</td>\n",
       "      <td>21.3194</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1401.05</td>\n",
       "      <td>350.360</td>\n",
       "      <td>55.0292</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>3381.35</td>\n",
       "      <td>360.830</td>\n",
       "      <td>269.7074</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>1401.05</td>\n",
       "      <td>376.520</td>\n",
       "      <td>28.4162</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0</td>\n",
       "      <td>1358.00</td>\n",
       "      <td>407.915</td>\n",
       "      <td>60.3518</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>1487.15</td>\n",
       "      <td>413.150</td>\n",
       "      <td>14.2226</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>1616.30</td>\n",
       "      <td>413.150</td>\n",
       "      <td>111.8036</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1</td>\n",
       "      <td>1960.70</td>\n",
       "      <td>413.150</td>\n",
       "      <td>35.5130</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0</td>\n",
       "      <td>1917.65</td>\n",
       "      <td>439.310</td>\n",
       "      <td>31.9646</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0</td>\n",
       "      <td>1616.30</td>\n",
       "      <td>491.630</td>\n",
       "      <td>69.2228</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0</td>\n",
       "      <td>1314.95</td>\n",
       "      <td>507.320</td>\n",
       "      <td>102.9326</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0</td>\n",
       "      <td>2520.35</td>\n",
       "      <td>517.790</td>\n",
       "      <td>180.9974</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1</td>\n",
       "      <td>3510.50</td>\n",
       "      <td>528.245</td>\n",
       "      <td>81.6422</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0</td>\n",
       "      <td>1616.30</td>\n",
       "      <td>549.170</td>\n",
       "      <td>134.8682</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1</td>\n",
       "      <td>3941.00</td>\n",
       "      <td>585.800</td>\n",
       "      <td>147.2876</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>1</td>\n",
       "      <td>2606.45</td>\n",
       "      <td>591.035</td>\n",
       "      <td>269.7074</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1</td>\n",
       "      <td>11259.50</td>\n",
       "      <td>601.490</td>\n",
       "      <td>766.4834</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0</td>\n",
       "      <td>1831.55</td>\n",
       "      <td>622.430</td>\n",
       "      <td>35.5130</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>1</td>\n",
       "      <td>1444.10</td>\n",
       "      <td>627.650</td>\n",
       "      <td>138.4166</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>0</td>\n",
       "      <td>2132.90</td>\n",
       "      <td>638.120</td>\n",
       "      <td>129.5456</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1</td>\n",
       "      <td>2606.45</td>\n",
       "      <td>643.355</td>\n",
       "      <td>221.8040</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>0</td>\n",
       "      <td>2046.80</td>\n",
       "      <td>664.280</td>\n",
       "      <td>147.2876</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>1</td>\n",
       "      <td>2778.65</td>\n",
       "      <td>674.750</td>\n",
       "      <td>275.0300</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>0</td>\n",
       "      <td>2305.10</td>\n",
       "      <td>690.440</td>\n",
       "      <td>90.5132</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>1</td>\n",
       "      <td>3381.35</td>\n",
       "      <td>695.675</td>\n",
       "      <td>299.8688</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>0</td>\n",
       "      <td>1831.55</td>\n",
       "      <td>700.910</td>\n",
       "      <td>269.7074</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>0</td>\n",
       "      <td>1573.25</td>\n",
       "      <td>747.995</td>\n",
       "      <td>127.7714</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>0</td>\n",
       "      <td>1874.60</td>\n",
       "      <td>805.550</td>\n",
       "      <td>310.5140</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>1</td>\n",
       "      <td>3424.40</td>\n",
       "      <td>884.030</td>\n",
       "      <td>672.4508</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>0</td>\n",
       "      <td>2821.70</td>\n",
       "      <td>899.720</td>\n",
       "      <td>220.0298</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>0</td>\n",
       "      <td>1616.30</td>\n",
       "      <td>915.410</td>\n",
       "      <td>360.1916</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>1</td>\n",
       "      <td>3682.70</td>\n",
       "      <td>941.570</td>\n",
       "      <td>670.6766</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>0</td>\n",
       "      <td>1228.85</td>\n",
       "      <td>962.510</td>\n",
       "      <td>110.0294</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>1</td>\n",
       "      <td>2391.20</td>\n",
       "      <td>978.200</td>\n",
       "      <td>330.0302</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>1</td>\n",
       "      <td>2735.60</td>\n",
       "      <td>978.200</td>\n",
       "      <td>104.7068</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>1</td>\n",
       "      <td>2175.95</td>\n",
       "      <td>983.435</td>\n",
       "      <td>253.7396</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>1</td>\n",
       "      <td>1142.75</td>\n",
       "      <td>1035.755</td>\n",
       "      <td>157.9328</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>1</td>\n",
       "      <td>1401.05</td>\n",
       "      <td>1077.605</td>\n",
       "      <td>76.3196</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>1</td>\n",
       "      <td>5835.20</td>\n",
       "      <td>1098.530</td>\n",
       "      <td>544.7084</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>1</td>\n",
       "      <td>3252.20</td>\n",
       "      <td>1145.630</td>\n",
       "      <td>993.5810</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>1</td>\n",
       "      <td>1573.25</td>\n",
       "      <td>1192.715</td>\n",
       "      <td>393.9014</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>1</td>\n",
       "      <td>3467.45</td>\n",
       "      <td>1245.035</td>\n",
       "      <td>516.3212</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>1</td>\n",
       "      <td>1401.05</td>\n",
       "      <td>1328.750</td>\n",
       "      <td>39.0614</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>1</td>\n",
       "      <td>1358.00</td>\n",
       "      <td>1401.995</td>\n",
       "      <td>344.2238</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>1</td>\n",
       "      <td>4242.35</td>\n",
       "      <td>1579.880</td>\n",
       "      <td>894.2258</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>1</td>\n",
       "      <td>1228.85</td>\n",
       "      <td>1684.520</td>\n",
       "      <td>166.8038</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    V1        V2        V3        V4  V5\n",
       "0    0   2520.35   277.115   51.4808  22\n",
       "1    0   1702.40   282.350   62.1260  51\n",
       "2    0   1917.65   345.125   21.3194  44\n",
       "3    0   1401.05   350.360   55.0292  42\n",
       "4    1   3381.35   360.830  269.7074  32\n",
       "5    0   1401.05   376.520   28.4162  24\n",
       "6    0   1358.00   407.915   60.3518  26\n",
       "7    0   1487.15   413.150   14.2226  41\n",
       "8    0   1616.30   413.150  111.8036  55\n",
       "9    1   1960.70   413.150   35.5130  71\n",
       "10   0   1917.65   439.310   31.9646  37\n",
       "11   0   1616.30   491.630   69.2228  55\n",
       "12   0   1314.95   507.320  102.9326  53\n",
       "13   0   2520.35   517.790  180.9974  42\n",
       "14   1   3510.50   528.245   81.6422  25\n",
       "15   0   1616.30   549.170  134.8682  25\n",
       "16   1   3941.00   585.800  147.2876  27\n",
       "17   1   2606.45   591.035  269.7074  37\n",
       "18   1  11259.50   601.490  766.4834  35\n",
       "19   0   1831.55   622.430   35.5130  51\n",
       "20   1   1444.10   627.650  138.4166  73\n",
       "21   0   2132.90   638.120  129.5456  49\n",
       "22   1   2606.45   643.355  221.8040  27\n",
       "23   0   2046.80   664.280  147.2876  31\n",
       "24   1   2778.65   674.750  275.0300  34\n",
       "25   0   2305.10   690.440   90.5132  41\n",
       "26   1   3381.35   695.675  299.8688  44\n",
       "27   0   1831.55   700.910  269.7074  40\n",
       "28   0   1573.25   747.995  127.7714  27\n",
       "29   0   1874.60   805.550  310.5140  42\n",
       "30   1   3424.40   884.030  672.4508  53\n",
       "31   0   2821.70   899.720  220.0298  38\n",
       "32   0   1616.30   915.410  360.1916  20\n",
       "33   1   3682.70   941.570  670.6766  37\n",
       "34   0   1228.85   962.510  110.0294  21\n",
       "35   1   2391.20   978.200  330.0302  69\n",
       "36   1   2735.60   978.200  104.7068  29\n",
       "37   1   2175.95   983.435  253.7396  19\n",
       "38   1   1142.75  1035.755  157.9328  25\n",
       "39   1   1401.05  1077.605   76.3196  57\n",
       "40   1   5835.20  1098.530  544.7084  51\n",
       "41   1   3252.20  1145.630  993.5810  27\n",
       "42   1   1573.25  1192.715  393.9014  28\n",
       "43   1   3467.45  1245.035  516.3212  40\n",
       "44   1   1401.05  1328.750   39.0614  49\n",
       "45   1   1358.00  1401.995  344.2238  39\n",
       "46   1   4242.35  1579.880  894.2258  37\n",
       "47   1   1228.85  1684.520  166.8038  41"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=pd.read_csv('数据5.1.csv')\n",
    "data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a3652f15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "交叉验证模型评分: 0.7571428571428571\n"
     ]
    }
   ],
   "source": [
    "# 将数据划分为特征变量和响应变量\n",
    "X = data.drop(['V1'],axis=1)#设置特征变量，即除V1之外的全部变量\n",
    "y = data['V1']#设置响应变量，即V1\n",
    "X_train, X_test, y_train, y_test =  train_test_split(X,y,test_size=0.3, stratify=y, random_state=123)\n",
    "\n",
    "# 使用交叉验证计算高斯朴素贝叶斯模型的评分\n",
    "gnb = GaussianNB()\n",
    "\n",
    "\n",
    "# 拟合模型\n",
    "gnb.fit(X_train, y_train)\n",
    "\n",
    "# 进行交叉验证，返回模型评分\n",
    "scores = cross_val_score(gnb, X_train, y_train, cv=5)\n",
    "\n",
    "# 输出模型评分\n",
    "print(\"交叉验证模型评分:\", np.mean(scores))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86a3851d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
